package org.dashboard.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.dashboard.bean.AdChart;
import org.dashboard.constant.Constant;
import org.dashboard.dao.AdChartMapper;
import org.dashboard.service.AdChartService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Service(interfaceClass = AdChartService.class)
@CacheConfig(cacheManager = Constant.DEFAULT_CACHE_MANAGER, cacheNames = "chart")
public class AdChartServiceImpl implements AdChartService {
    private static final Logger logger = LoggerFactory.getLogger(AdChartServiceImpl.class);


    @Autowired
    AdChartMapper adChartMapper;

    @Override
    @CacheEvict(key = "'chartId:' + #chartId.toString()")
    public int deleteByPrimaryKey(Integer chartId) {
        return adChartMapper.deleteByPrimaryKey(chartId);
    }

    @Override
    @CacheEvict(key = "'chartId:' + #record.chartId.toString()")
    public int insert(AdChart record) {
        adChartMapper.insert(record);
        return record.getChartId();
    }

    @Override
    @CacheEvict(key = "'chartId:' + #record.chartId.toString()")
    public int insertSelective(AdChart record) {
        adChartMapper.insertSelective(record);
        return record.getChartId();
    }

    @Override
    @Cacheable(key = "'chartId:' + #chartId.toString()")
    public AdChart selectByPrimaryKey(Integer chartId) {
        return adChartMapper.selectByPrimaryKey(chartId);
    }

    @Override
    @CacheEvict(key = "'chartId:' + #record.chartId.toString()")
    public int updateByPrimaryKeySelective(AdChart record) {
        return adChartMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    @CacheEvict(key = "'chartId:' + #record.chartId.toString()")
    public int updateByPrimaryKeyWithBLOBs(AdChart record) {
        return adChartMapper.updateByPrimaryKeyWithBLOBs(record);
    }

    @Override
    @CacheEvict(key = "'chartId:' + #record.chartId.toString()")
    public int updateByPrimaryKey(AdChart record) {
        return adChartMapper.updateByPrimaryKey(record);
    }

    @Override
    public List<AdChart> selectAdCharts() {
        return adChartMapper.selectAdCharts();
    }

    @Override
    @Transactional
    @CacheEvict(allEntries = true)
    public boolean deleteByPrimaryKeys(ArrayList<Integer> chartIds) {
        for (Integer id : chartIds) {
            adChartMapper.deleteByPrimaryKey(id);
        }
        return true;
    }

    @Override
    public List<Map<String, Object>> loadUserRoleCharts(Integer userId, Integer resourceType) {
        return adChartMapper.loadUserRoleCharts(userId, resourceType);
    }

    @Override
    public PageInfo selectAdChartsPaged(String criteria, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<AdChart> adCharts = adChartMapper.selectAdChartsSearched(criteria);
        return new PageInfo<>(adCharts);
    }
}
